package sv.main;

import sv.view.*;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.InetSocketAddress;
import sv.net2.Receiver;
import sv.net2.NetConnection;

public class MyNetwork implements Receiver {

	private Game game;
	private Tile[][] ownTile;
	private Tile[][] oppTile;
	private JTextArea log;
	private NetConnection con;
	private byte hpSum;
	
	public MyNetwork() {
		//con = new NetConnection();
		hpSum = 17;
	}
	
	public MyNetwork(Game game, JTextArea log){
		this.game = game;
		this.log = log;
		hpSum = 17;
	}
	
	public void setOwnTile(Tile[][] ownTile){
		this.ownTile = ownTile; 
	}
	
	public void setOppTile(Tile[][] oppTile){
		this.oppTile = oppTile;
	}
	
	public void startServer() {
		con = new NetConnection(true, new InetSocketAddress(2312), this);
		new Thread(con).start();
	}
	
	public void connectTo(String host) {
		con = new NetConnection(false, new InetSocketAddress(host, 2312), this);
		new Thread(con).start();
	}
	
	
	
	/**
	 * Returns the NetConnection.
	 */
	
	public NetConnection connection() {
		return con;
	}
	
	public void ready() {
		System.out.println("RECEIVED: ready ()");
		log.append("opponent is ready\n");
		game.setOppReady(true);
		if (game.isSelfReady()){
			log.append("all players ready\n");
			log.append("let the battle begin\n");
			game.setAllReady(true);
		}
	}
	
	public void shoot(byte field) {
		System.out.println("RECEIVED: shoot (" + field + ")");
		
		byte x;
		byte y;
		x = (byte)(field/10);
		y = (byte)(field % 10);
		
		game.decShots();
		if (game.getTile(x, y) == Game.State.SHIP){
			if (game.getShipHp(game.getShipId(x, y)) == 1){
				con.sendShipDestroyed(field);
				log.append("one of your ships has been destroyed\n");
				if (hpSum == 1){
					log.append("all your ships were destroyed\n");
					log.append("you lose !\n");
					con.sendIAmLoser();
					game.iLose();
				}
			}
			else {
				con.sendHitShip(field);
			}
			game.changeTile(x, y, Game.State.HITSHIP);
			game.shipHit(game.getShipId(x, y));
			ownTile[y][x].setBackground(Color.red);
			hpSum--;
		}
		else {
			if (game.getTile(x, y) != Game.State.HITSHIP){
				con.sendHitWater(field);
				game.changeTile(x, y, Game.State.HITWATER);
				ownTile[y][x].setBackground(Color.gray);
			}
		}
	}
	
	public void hitWater(byte field) {
		System.out.println("RECEIVED: hit_water (" + field + ")");
		
		byte hpSum = 0;
		byte x;
		byte y;
		y = (byte)(field/10);
		x = (byte)(field % 10);
		
		oppTile[x][y].setBackground(Color.gray);
	}
	
	public void hitShip(byte field) {
		System.out.println("RECEIVED: hit_ship (" + field + ")");
		
		byte hpSum = 0;
		byte x;
		byte y;
		y = (byte)(field/10);
		x = (byte)(field % 10);
		
		log.append("enemy ship hit\n");
		oppTile[x][y].setBackground(Color.red);
	}
	
	public void destroyedShip(byte field) {
		System.out.println("RECEIVED: destroyed_ship (" + field + ")");
		
		byte hpSum = 0;
		byte x;
		byte y;
		y = (byte)(field/10);
		x = (byte)(field % 10);
		
		log.append("enemy ship destroyed\n");
		oppTile[x][y].setBackground(Color.red);
	}
	
	public void lostGame() {
		System.out.println("RECEIVED: i_am_loser ()");
		
		log.append("all enemey ships destroyed.\n");
		log.append("you win !\n");
	}
	
	public synchronized void criticalError(Exception e) {
		log.append("critical error: " + e.toString());
	}
}
